﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VehicleRecordSystem.util;

namespace VehicleRecordSystem.view
{
    /// <summary>
    /// Interaction logic for licensePage.xaml
    /// </summary>
    /// 


    public partial class licensePage : Page
    {
    private static licensePage instanse;

        public static licensePage Instanse
        {
            get
            {
                if (instanse == null)
                {
                    instanse = new licensePage();
                }

                return instanse;

            }
        }

        public licensePage()
        {
            InitializeComponent();
        }
        private void btnLicensePageSave_Click(object sender, RoutedEventArgs e)
        {
            if (validate())
            {
                String license_number = txtlicenseNumber.Text;
                String validity_period = txtValidityPeriod.Text;
                String license_class=txtClass.Text;
                String owner_name = txtOwnerName.Text;
                Single unladen_weight = Convert.ToSingle(txtUnladenWeight.Text);
                Int16 seats = Convert.ToInt16(txtSeats.Text);
                Single annual_fee = Convert.ToSingle(txtAnnualFee.Text);
                Single arreas = Convert.ToSingle(txtArreas.Text);
                Single total = Convert.ToSingle(txtTotal.Text);
                Single env_control_lavy = Convert.ToSingle(txtEnvControlLevy.Text);
                DateTime? valid_from = datePicketValidFrom.SelectedDate;
                DateTime? valid_to = datePickerValidTo.SelectedDate;
                DateTime? issue_date = datepickerIssueDate.SelectedDate;
                String province = txtProvince.Text;
                String emmission_test_number = txtEmissionTestNumber.Text;
                BitmapImage emmission_test_image = new BitmapImage(new Uri(txtEmissionTestImagePath.Text));
                BitmapImage license_image = new BitmapImage(new Uri(txtLicenseImagePath.Text));
                
            }
        }
        private Boolean validate()
        {
            Boolean ok = true;

            if(string.IsNullOrEmpty(txtlicenseNumber.Text)){
                ok=false;
                GUI.changeBackgroundColorToError(txtlicenseNumber);
            }
            if(string.IsNullOrEmpty(txtValidityPeriod.Text)){
                ok=false;
                GUI.changeBackgroundColorToError(txtValidityPeriod);
            }
            if(string.IsNullOrEmpty(txtClass.Text)){
                ok=false;
                GUI.changeBackgroundColorToError(txtClass);
            }
            if(string.IsNullOrEmpty(txtOwnerName.Text)){
                ok=false;
                GUI.changeBackgroundColorToError(txtOwnerName);
            }
            if (string.IsNullOrEmpty(txtUnladenWeight.Text) || !VRSValidate.isNumberic(txtUnladenWeight.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtUnladenWeight);
            }

            if (string.IsNullOrEmpty(txtSeats.Text) || !VRSValidate.isInteger(txtSeats.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtSeats);
            }
            if (string.IsNullOrEmpty(txtAnnualFee.Text) || !VRSValidate.isNumberic(txtAnnualFee.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtAnnualFee);
            }
            if (string.IsNullOrEmpty(txtArreas.Text) || !VRSValidate.isNumberic(txtArreas.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtArreas);
            }
            if (string.IsNullOrEmpty(txtTotal.Text) || !VRSValidate.isNumberic(txtTotal.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtTotal);
            }
            if (string.IsNullOrEmpty(txtEnvControlLevy.Text) || !VRSValidate.isNumberic(txtEnvControlLevy.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtEnvControlLevy);
            }
            if (string.IsNullOrEmpty(datePicketValidFrom.SelectedDate.ToString()))
            {
                ok = false;
                GUI.changeBackgroundColorToError(datePicketValidFrom);
            }
            if (string.IsNullOrEmpty(datePickerValidTo.SelectedDate.ToString()))
            {
                ok = false;
                GUI.changeBackgroundColorToError(datePickerValidTo);
            }
            if (string.IsNullOrEmpty(datepickerIssueDate.SelectedDate.ToString()))
            {
                ok = false;
                GUI.changeBackgroundColorToError(datepickerIssueDate);
            }
            if (string.IsNullOrEmpty(txtProvince.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtProvince);
            }
            if (string.IsNullOrEmpty(txtEmissionTestNumber.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtEmissionTestNumber);
            }
            if (!VRSValidate.validUri(txtEmissionTestImagePath.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtEmissionTestImagePath);
            }
            if (!VRSValidate.validUri(txtLicenseImagePath.Text))
            {
                ok = false;
                GUI.changeBackgroundColorToError(txtLicenseImagePath);
            }
            return ok;
        }
        private void changeBackgroundColorToNormal(object sender, RoutedEventArgs e)
        {
            GUI.changeBackgroundColorToNormal(sender);
        }

        private void btnEmissionTestImageBrows_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.ShowDialog();
                openFileDialog.Title = "Set Insurance Card Image";
                openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
                openFileDialog.DefaultExt = ".jpeg";
                txtEmissionTestImagePath.Text = openFileDialog.FileName;

                ImageSource imageSource = new BitmapImage(new Uri(txtEmissionTestImagePath.Text));
            }
            catch (Exception)
            {
                MessageBox.Show("Image Uploading Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnCertificateBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
                openFileDialog.ShowDialog();
                openFileDialog.Title = "Set Insurance Card Image";
                openFileDialog.Filter = "All supported graphics|*.jpg;*.jpeg;*.png|" + "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" + "Portable Network Graphic (*.png)|*.png";
                openFileDialog.DefaultExt = ".jpeg";
                txtLicenseImagePath.Text = openFileDialog.FileName;

                ImageSource imageSource = new BitmapImage(new Uri(txtLicenseImagePath.Text));
            }
            catch (Exception)
            {
                MessageBox.Show("Image Uploading Error", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
        private void validate_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            if (!VRSValidate.isNumberic(tb.Text))
            {
                GUI.changeBackgroundColorToError(tb);
            }
            else
            {
                GUI.changeBackgroundColorToNormal(tb);
            }
        }
        
    }
        
}
